Golang : Reclaim memory occupied by make() example
Problem :
You have allocated some memory for buffer via the make()
function and you want to reclaim back the occupied memory after done using the buffer. How to do that?
Solution :
For example :
buffer := make([]byte, 1024)
or
buffer := make([][]int, row)
to reclaim back the memory, simply do :
buffer = nil
Golang's garbage collector will automatically free up the memory or you can trigger garbage collection manually as well with runtime/debug.FreeOSMemory()
References :
https://www.socketloop.com/tutorials/golang-how-to-get-garbage-collection-data
https://www.socketloop.com/tutorials/golang-when-to-use-make-or-new
See also : Golang : When to use make or new?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10k Golang : Sort and reverse sort a slice of integers
+7.6k SSL : How to check if current certificate is sha1 or sha2 from command line
+18.9k Golang : Delete duplicate items from a slice/array
+12.9k Swift : Convert (cast) Int or int32 value to CGFloat
+26.2k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+9.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+5.5k Unix/Linux/MacOSx : How to remove an environment variable ?
+7.6k Golang : Rename part of filename
+9.5k Golang : Timeout example
+43.7k Golang : Get hardware information such as disk, memory and CPU usage
+6.6k Golang : Totalize or add-up an array or slice example